<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 关注 model
 *
 */
class Attentionsmodel extends CI_Model {

	function __construct()
    {
        parent::__construct();
    }
    
    /**
     * 添加关注
     *
     * @param unknown_type $uid
     * @param unknown_type $attentionid
     */
	function insert_attention($uid='', $attentionid='')
	{
		$data = array('uid'	=> $uid, 'attentionid'	=> $attentionid);
		if($this->db->insert('attentions', $data)){
			//同时添加为粉丝
			return $this->db->insert('fans', array('uid' => $attentionid, 'fanid' => $uid));
		}
		return false;
	}
	
	/**
	 * 取消关注
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $attentionid
	 */
	function delete_attention($uid='', $attentionid='')
	{
		if($this->db->delete('attentions', array('uid'=>$uid, 'attentionid'=>$attentionid))){
			//同时解除粉丝关系
			return $this->db->delete('fans', array('uid'=>$attentionid, 'fanid'=> $uid));
		}
		return false;
	}
	
	/**
	 * 取得关注信息
	 *
	 * @param unknown_type $uid
	 */
	function get_attentions($uid='')
	{
		/*$this->db->select('attentionid');
		$query = $this->db->get_where('attentions', array('uid'	=> $uid));
		if ($query->num_rows()>0) {
			foreach ($query->result_array() as $row){
				$result[] = $row['attentionid'];
			}
			return $result;
		}
		return false;*/
		
		$result = array();
    	$this->dblib->set_query_limit();
    	$this->db->select('user.id as uid, user.name, user.photo, user.reg_type, a.createtime')
				->from('user')
				->join('attentions as a', 'user.id=a.attentionid')
				->where('a.uid', $uid)
				->order_by('createtime', 'desc');
		$query = $this->db->get();
		if ($query->num_rows() > 0) {
			//如果有结果集
			$this->load->library('uploadimglib');
			$result = $query->result_array();
			//设置photo路径
			foreach ($result as $k=>$v){
				$result[$k]['photo'] = $this->uploadimglib->re_user_photo($v['photo'], $v['reg_type']);
				unset($result[$k]['reg_type']);//不需要此字段
			}
		}
		return $result;
	}
	
	/**
	 * 检测要关注的用户此前是否已经关注过了
	 *
	 * @param unknown_type $id
	 * @param unknown_type $attentionid
	 */
	function check_attention($uid='', $attentionid='')
	{
		$this->db->select('count(*) as count');
		$this->db->limit(1);
		$query = $this->db->get_where('attentions', array('uid' => $uid, 'attentionid' => $attentionid));
		$row = $query->row_array();
		return $row['count'] > 0;
	}
	
	/**
	 * 获取我关注的人的uid
	 *
	 * @param unknown_type $uid
	 */
	function get_myattentions_user_id($uid='')
	{
		$this->db->select('attentionid')
			 	 ->from('attentions')
			 	 ->where('uid', $uid);
		$query = $this->db->get();
		return $query->result_array();
	}
}

/* End of file attentionsmodel.php */
/* Location: ./application/models/attentionsmodel.php */